package com.yishu.servlet;

import com.yishu.Dao.UserDao;
import com.yishu.entity.User;
import com.yishu.util.DBConnection;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.util.List;

public class ShowUserListServlet extends HttpServlet {

    UserDao userDao = new UserDao();
    DBConnection dbConnection = new DBConnection();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            Connection connection = dbConnection.getCon();
            List<User> userList = userDao.showUserList(connection);
            dbConnection.closeCon(connection);

            if (userList != null) {
                req.setAttribute("userList", userList);
                req.getRequestDispatcher("userList.jsp").forward(req, resp);
            } else {
                req.setAttribute("result", "获取用户列表失败");
                req.getRequestDispatcher("userList.jsp").forward(req,resp);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        super.doGet(req, resp);
    }
}
